<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Structure Notation
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial025.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc52">4.1</A>&nbsp;&nbsp;Structure Notation</H2>
<A NAME="structures"></A>
In ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP>, structure fields can be given names.
This makes it possible to write structures in a
more readable and maintainable way.
Such structures first need to be declared by specifying a template like: 

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
:- local struct( book(author, title, year, publisher) ).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
Structures with the functor book/4 can then be written as 
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
book{}
book{title:'tom sawyer'}
book{title:'tom sawyer', year:1876, author:twain}
</PRE></BLOCKQUOTE>
which, in canonical syntax, correspond to the following:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
book(_, _, _, _)
book(_, 'tom sawyer', _, _)
book(twain, 'tom sawyer', 1876, _)
</PRE></BLOCKQUOTE>
There is absolutely no semantic difference between the two syntactical forms.
The special struct-syntax with names has the advantage that
<UL CLASS="itemize"><LI CLASS="li-itemize">
the arguments can be written in any order
<LI CLASS="li-itemize">&#8220;dummy&#8221; arguments with anonymous variables do not need to be written
<LI CLASS="li-itemize">the arity of the structure is not implied (and can be changed
by changing the declaration and recompiling the program)
</UL>
Sometimes it is necessary to refer to the numerical position of a
structure field within the structure, e.g. in the arg/3 predicate:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
arg(3, B, Y)
</PRE></BLOCKQUOTE>
When the structure has been declared as above, we can write instead:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
arg(year of book, B, Y)
</PRE></BLOCKQUOTE>
Declared structures help readability, and make programs easier to modify.
In order not to lose these benefits, one should always use curly-bracket and
of-syntax when working with them, and never write them in canonical
syntax or referring to argument positions numerically.
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>&#8857;</B><DD CLASS="dd-description"> <FONT COLOR="#9832CC">See also the
</FONT><A HREF="../bips/kernel/termmanip/update_struct-4.html"><FONT COLOR="#9832CC"><B>update_struct/4</B></FONT></A><A NAME="@default83"></A><FONT COLOR="#9832CC">
built-in predicate.</FONT>
</DL>

<HR>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial025.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
